package painting;

import directory.SeeDirectory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class BatchGenerationOfBaJiImages {
    public static void GenerateA4s(String folderPath, String A4) {
        List<String> baJiImages = SeeDirectory.seeImage(folderPath);


        if (baJiImages != null) {
            int num = baJiImages.size() % 8 == 0 ? 0 : 1;
            String[][] images = new String[baJiImages.size() / 8 + num][8];

            a:
            for (int i = 0; i < images.length; i++) {
                for (int j = 0; j < images[i].length; j++) {
                    if (baJiImages.isEmpty()) break a;
                    images[i][j] = baJiImages.remove(0);
                }
            }

            List<BufferedImage> A4s = new ArrayList<BufferedImage>();
            for (String[] image : images) {
                A4s.add(GenerateBaJiImage.getImage(image));
            }

            saveA4s(A4, A4s);
        }
    }

    public static void saveA4s(String folderPath, List<BufferedImage> A4s) {
        int count = 0;
        for (BufferedImage image : A4s) {
            // 将 BufferedImage 保存为图片文件
            try {
                // 修改保存路径为 D 盘根目录下的 A4_Circles_With_Images.png
                File output = new File(folderPath + "/BaJi" + count + ".png");
                ImageIO.write(image, "png", output);
                System.out.println("A4 纸上圆的绘制完成！");
            } catch (Exception e) {
                System.out.println("在 A4 纸上绘制圆时出错：" + e.getMessage());
            }
            count++;
        }
    }
}
